home *** CD-ROM | disk | FTP | other *** search
- Path: news.cis.nctu.edu.tw!usenet
- From: terryt@mcs.com (Terry Trippany)
- Newsgroups: comp.lang.c++
- Subject: Re: Newbie - inheritance ques!
- Date: 9 Jan 1996 21:32:46 GMT
- Organization: STR/Baxter Labs
- Message-ID: <4cumtv$sso@news.cis.nctu.edu.tw>
- References: <madhavi.820163040@class1.iastate.edu> <4bvocb$5k1@werple.net.au> <30E41252.35D5@ix.netcom.com>
- NNTP-Posting-Host: @159.198.73.111
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <30E41252.35D5@ix.netcom.com>, susaned@ix.netcom.com
- says...
- >
- >David White wrote:
- >>
- >> madhavi@iastate.edu (Madhavi S Nuttaki) writes:
- >>
- >> >Hi,
- >>
- >> > I am trying to figure out what is happening in the
- following
- >> >scenario.
- >>
- >> > class1 class2
- >> > ^
- >> > |
- >> > class3
- >>
- >> >class3 inherits from class2.
- >> >class1 contains a class2 object.
- >>
- >> >I overloaded the '<' operator (defined int class3, virtual in
- >> >class2).
- >>
- >> >Lets say class1 has a data element - class2 *tmp;
- >>
- >> > In one of class1's methods I (which receives a class2
- *tmp1)
- >> >do this:
- >>
- >> > it(tmp < tmp1) - now this should call the
- overloaded
- >> >function defined in class3 (according to my understanding).
- >>
- >> > But this is not happening. I am having to explicitly call
- hte
- >> >overloaded func.
- >>
- >> >Can anyone help me figure this out.
- >>
- >> The code above is comparing two pointers, not two objects.
- >> Change it to: if(*tmp < *tmp1).
- >>
- >> David White
- >> davidw@werple.mira.net.au
- >
- Hello all,
-
- One of the first things that should be done to make things simpler
- is to eliminate the pointers and concentrate on the virtual
- inheritence. Simply put, classes of type 2 will use its' definition
- of the operator and classes of type 3 will use the overloaded one.
- Virtuals get tricky when you aren't careful using multiple
- inheritance. You have to watch the type of classes on the left hand
- side of this operator.
-
- eg: take the following simple example:
-
- #include <fstream.h>
-
- class y
- {
- public :
- virtual int operator <(y) { cout << "Y Virt\n"; return 1;};
- };
-
- class z : public y
- {
- public:
- int operator <(y) { cout << "Z overload\n"; return 2;};
- };
-
- class w : public y
- {
-
- };
-
- class x
- {
- y myY;
- public :
- void testVirtual(y yIn){ myY < yIn;}; // 2 of type Y
- };
-
- int main()
- {
- y myYclass;
- x myXclass;
- z myZclass;
- w usesVirt;
-
- myXclass.testVirtual(myYclass); // will use virtual Y
- myZclass < myYclass; // will use Z overload
- myZclass < usesVirt; // will use z overload
- usesVirt < myZclass; // will use inherited from y
-
- return 0;
- }
-
-
-
- The following is the output:
- Y Virt
- Z overload
- Z overload
- Y Virt
-
- Sometimes it is a good idea to make your classes streamable for
- debugging purposes. Later,
-
- Terry Trippany
- Strategic Technology Resources
- Chicago, IL
-
- @#?#%% Compiler
-
-
-
-
-
-